package com.udi.project.business.local.gs1.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.udi.common.utils.security.ShiroUtils;
import com.udi.common.utils.text.Convert;
import com.udi.project.business.local.gs1.daomain.AvailableGs1;
import com.udi.project.business.local.gs1.domain.BeUsable;
import com.udi.project.business.local.gs1.domain.Gs1Data;
import com.udi.project.business.local.gs1.domain.SelGsMesVo;
import com.udi.project.business.local.gs1.mapper.Gs1GenerateMapper;
import com.udi.project.business.local.gs1.mapper.Gs1MangeMapper;
import com.udi.project.business.local.gs1.service.Gs1MangeService;
import com.udi.project.business.pojo.damain.Gs1Udi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;


@Service
@Slf4j
public class Gs1MangeServiceImpl implements Gs1MangeService {

    @Autowired
    private Gs1MangeMapper gs1MangeMapper;
    @Autowired
    private Gs1GenerateMapper gs1GenerateMapper;

    /**
     * 根据id查询udi信息
     *
     * @param id
     * @return java.util.List<com.udi.project.business.local.gs1.domain.SelGsMesVo>
     * @author yuxin
     * @creed: Talk is cheap,show me the code
     * @date 2021/10/15 8:55
     */
    @Override
    public SelGsMesVo selectGs1ById(Integer id) {
        return gs1MangeMapper.getGS1(id);
    }

    @Override
    public List<SelGsMesVo> selectUdi(SelGsMesVo selGsMesVo) {
        selGsMesVo.setDelFlag("0");
        //获取当前用户数据
        JSONObject current = JSONObject.parseObject(JSONObject.toJSON(ShiroUtils.getSysUser()).toString());
        String deptId = ((JSONObject)current.get("dept")).getString("deptId");
        //获取权限标识符
        String roleKey =((JSONObject)((JSONArray)current.get("roles")).get(0)).getString("roleKey");
        if(roleKey.contains("admin")){
            return gs1MangeMapper.selectUdi(selGsMesVo);
        }else{
            selGsMesVo.setDeptId(deptId);
            return gs1MangeMapper.selectUdi(selGsMesVo);
        }
    }

    /**
     * @param selGsMesVo ai_id
     * @Description: 查询所有UDI历史信息
     * @return: java.util.List<com.udi.project.business.pojo.damain.Gs1Udi>
     * @Date: 2021/8/18 17:29
     */
    @Override
    public List<SelGsMesVo> selectUdils(SelGsMesVo selGsMesVo) {
        selGsMesVo.setDelFlag("1");
        //获取当前用户数据
        JSONObject current = JSONObject.parseObject(JSONObject.toJSON(ShiroUtils.getSysUser()).toString());
        String deptId = ((JSONObject)current.get("dept")).getString("deptId");
        //获取权限标识符
        String roleKey =((JSONObject)((JSONArray)current.get("roles")).get(0)).getString("roleKey");
        if(roleKey.contains("admin")){
            return gs1MangeMapper.selectUdi(selGsMesVo);
        }
        selGsMesVo.setDeptId(deptId);
        return gs1MangeMapper.selectUdi(selGsMesVo);
    }

    /**
     * GS1-128编辑
     *
     * @param selGsMesVo [gs1Data]* @return int
     * @author yuxin
     * @creed: Talk is cheap,show me the code
     * @date 2021/10/15 14:10
     */
    @Override
    public int updateGs1(SelGsMesVo selGsMesVo) {
        return gs1MangeMapper.updateGs1(selGsMesVo);
    }

    /**
     * 查询应用标识符
     *
     * @return java.util.List<com.udi.project.business.local.gs1.domain.Gs1Data>
     * @author yuxin
     * @creed: Talk is cheap,show me the code
     * @date 2021/10/15 14:28
     */
    @Override
    public List<Gs1Data> getAi() {
        return gs1GenerateMapper.getaiInfo();
    }

    /**
     * 删除gs1-128
     * @param ids [ids]* @return int
     * @author yuxin
     * @creed: Talk is cheap,show me the code
     * @date 2021/10/15 15:16
     */
    @Override
    public int remove(String ids) {
        Integer[] piIds = Convert.toIntArray(ids);
        return gs1MangeMapper.remove(piIds);
    }

    /**
     * 永久删除gs1-128
     *
     * @param ids [ids]* @return int
     * @author yuxin
     * @creed: Talk is cheap,show me the code
     * @date 2021/10/15 15:16
     */
    @Override
    public int delete(String ids) {
        Integer[] piIds = Convert.toIntArray(ids);
        return gs1MangeMapper.delete(piIds);
    }


    /**  GS1打印交互*/
    @Override
    public List<AvailableGs1> selectGS1() {
        return gs1MangeMapper.selectGS1();
    }

    /**
     * @param id ai_id
     * @Description: 查询所有UDI带DI
     * @return: java.util.List<com.udi.project.business.pojo.damain.Gs1Udi>
     * @Date: 2021/8/18 17:30
     */
    @Override
    public List<Gs1Udi> selectUdiDi(String id) {
        return null;
    }


    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
    @Override
    public String deleteUdi(List<String> corp) {
        int ims = gs1MangeMapper.deleteUdi(corp);
        if(ims == 0){
            log.info("临床尺寸信息删除失败");
            return "删除失败";
        }
        return "操作成功";
    }

    /**
     * @param name 产品
     * @Description: 模糊查询di
     * @return: 产品名称，规格型号，di码，di码对应的di_id
     * @Date: 2021/8/20 9:41
     * http://localhost:8080/Gs1Generate/getdi?cpmctymc=产品&ggxh=规格
     */
    @Override
    public List<BeUsable> getDi(String name) {
        return gs1MangeMapper.getDi(name);
    }
}
